#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>

#define SIZE 30
#define PORT 5566
#define FILENAME "1.xml"
int main(){
  int socketfd;
  int ret;

  struct stat buf_pic;
  stat(FILENAME,&buf_pic);
  printf("file size = %d\n",buf_pic.st_size);
  int data_size= buf_pic.st_size;
  char data_suffix[10] = "1.xml";
  char buffer[SIZE];
  char *buffer_temp = (char*)malloc(sizeof(char)*data_size);
  FILE* fd = fopen(FILENAME,"rb+");
  if(fd<0){perror("wrong file");}
  fread(buffer_temp,sizeof(char),data_size,fd);
  //printf("%s\n",buffer_temp);
  struct sockaddr_in address;
  struct hostent* host;
  struct netent* net;
  //char* www ="www.taobao.com";
  //host = gethostbyname(www);
  //net = getnetbyname("www.baidu.com");
  //printf(" %s\n",host->h_name);
  //char** alias;

 // for(alias =host->h_aliases;*alias!=NULL;alias++)
 //   printf("%s,\n",*alias);

 //for(alias = host->h_addr_list;*alias!=NULL;alias++)
 //   printf("%s,",inet_ntoa(*(struct in_addr*)(*alias)));
  

  socketfd = socket(AF_INET,SOCK_STREAM,0);//创建一个套接字

  address.sin_family = AF_INET;//设置为因特网协议
  address.sin_port = htons(PORT);//设置端口号
  inet_pton(AF_INET,"127.0.0.1",&address.sin_addr.s_addr);//设置ip地址

  ret=connect(socketfd,(struct sockaddr*)&address,sizeof(address));//连接服务器

  if(ret == -1){
    printf("can't connect!\n");
    exit(0);
  }
  int n=0;
    
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"set");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send set error\n");
 
  int setting = (1<<10)|(2<<8)|(3<<6)|(3<<4)|(2<<2)|1;
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"%d",setting);
  printf("setting send = %s\n",buffer);
  n=send(socketfd,buffer,sizeof(buffer),0);
  if(n<0)perror("send size error\n");

  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 

  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"prm_num");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send set error\n");
 
  int prm_num = 1;
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"%d",prm_num);
  printf("prm_num send = %s\n",buffer);
  n=send(socketfd,buffer,sizeof(buffer),0);
  if(n<0)perror("send size error\n");

  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  }

  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"data_size");
  printf("size\n");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send size error\n");
 
   //  sleep(10);
  memset(buffer,0,sizeof(buffer));
    sprintf(buffer," %-14d%s",data_size,data_suffix);
    printf("data size send = %s\n",buffer);
     n=send(socketfd,buffer,sizeof(buffer),0);
    if(n<0)perror("send size error\n");

 // int n=0;
 // n=read(socketfd,buffer,sizeof(buffer));
  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 
   
  printf("now send data:");
  fgets(buffer,sizeof(buffer),stdin);
  
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"data");
  printf("data\n");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send data error\n");

  int num=0;
  //printf("%s",buffer_temp);
  num = send(socketfd,buffer_temp,data_size,MSG_WAITALL);//把数据传给服务器
  if(num<0){perror("write data error!");}
  printf("num = %d\n",num);
  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 
//  FILE *fd2 = fopen("3.jpg","wb+");
//  fwrite(buffer_temp,sizeof(char),num,fd2);
  fclose(fd);
//  fclose(fd2);
  sprintf(buffer,"name DONE");
  //num = send(socketfd,buffer,sizeof(buffer),0);//把数据传给服务器
  //if(num<0){perror("write data error!");}
  close(socketfd);//关闭套接字
  return 0;
}

